package com.micro.frog.fortune.sdk.cwl.impl;

import cn.hutool.core.util.URLUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.micro.frog.fortune.sdk.cwl.CwlApi;
import com.micro.frog.fortune.sdk.cwl.model.CwlProp;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiReq;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiResp;
import java.nio.charset.Charset;

/**
 * @author jason.lv
 * @since 2022/2/22 17:54
 */
public class CwlHttp implements CwlApi {

    private final CwlProp prop;

    public CwlHttp(CwlProp prop) {
        this.prop = prop;
    }

    @Override
    public CwlApiResp list(CwlApiReq req) {

        // 先拿Cookie
        HttpRequest request = HttpUtil.createGet(prop.getDomain());
        request.header(Header.ACCEPT, ContentType.TEXT_HTML.getValue());
        HttpResponse response = request.execute();
        String cookie = response.getCookieStr();

        // 查数据
        String query = URLUtil.buildQuery(JSONUtil.parseObj(req), Charset.defaultCharset());
        String url = prop.url("/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice", query);
        request = HttpUtil.createGet(url);
        request.header(Header.ACCEPT, ContentType.JSON.getValue());
        request.header(Header.COOKIE, cookie);
        response = request.execute();
        String body = response.body();
        return JSONUtil.toBean(body, CwlApiResp.class);
    }
}
